//app.js
const http = require("./utils/http")
const wxToast = require("./utils/wx-toast")
const toPromise = require("./utils/to-promise")

App({
    onLaunch: function() {
        const updateManager = wx.getUpdateManager()
        updateManager.onCheckForUpdate(res => {
            // 请求完新版本信息的回调
            console.log(res.hasUpdate)
        })

        updateManager.onUpdateReady(() => {
            wx.showModal({
                title: '更新提示',
                content: '新版本已经准备好，是否重启应用？',
                confirmColor: '#4db4ff',
                success: res => {
                    if (res.confirm) {
                        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                        updateManager.applyUpdate()
                    }
                }
            })
        })

        updateManager.onUpdateFailed(function() {
            // 新版本下载失败
        })
        this.hidetabbar()
        this.getSystemInfo()
    },
    http,
    wxToast,
    toPromise,
    onShow() {
        setTimeout(() => {
            this.loadConfig()
        }, 100)

    },
    hidetabbar() {
        wx.hideTabBar({
            fail: function() {
                setTimeout(function() { // 做了个延时重试一次，作为保底。
                    wx.hideTabBar()
                }, 500)
            }
        });
    },
    getSystemInfo: function() {
        let t = this;
        wx.getSystemInfo({
            success: function(res) {
                t.globalData.systemInfo = res;
            }
        });
    },
    editTabbar: function() {
        let tabbar = this.globalData.tabBar;
        let currentPages = getCurrentPages();
        let _this = currentPages[currentPages.length - 1];
        let pagePath = _this.route;
        (pagePath.indexOf('/') != 0) && (pagePath = '/' + pagePath);
        for (let i in tabbar.list) {
            tabbar.list[i].selected = false;
            (tabbar.list[i].pagePath == pagePath) && (tabbar.list[i].selected = true);
        }
        _this.setData({
            tabbar: tabbar
        });
    },
    getPrevPage() {
        const ps = getCurrentPages()
        if (ps.length < 2) {
            return null
        }
        return ps[ps.length - 2]
    },
    globalData: {
        deposit: null, //保证
        systemInfo: null, //客户端设备信息
        key: "405b13b07a486ebd903ba535faefa811", //高德地图key
        tabBar: {
            "backgroundColor": "#ffffff",
            "color": "#979795",
            "selectedColor": "#2691df",
            "list": [{
                    "pagePath": "/pages/agency/index",
                    "iconPath": "/assets/icon/home.png",
                    "selectedIconPath": "/assets/icon/home2.png",
                    "text": "首页"
                },
                {
                    "pagePath": "",
                    "iconPath": "/assets/icon/photo.png",
                    "isSpecial": true,
                    "text": "随手拍"
                },
                {
                    "pagePath": "/pages/agency/personal/personal",
                    "iconPath": "/assets/icon/personal.png",
                    "selectedIconPath": "/assets/icon/personal2.png",
                    "text": "我的"
                }
            ]
        }
    },
    loadConfig() {
        http.post('rest/config/queryByList.do').then(res => {
            res.obj.map(o => {
                wx.setStorage({
                    key: o.key,
                    data: o.value,
                })
            })
        })
    },
    getFormId(formIdArr = []) {
        http.post('rest/push/batchSave.do', {
            data: JSON.stringify(formIdArr),
        })
    },


    // 获取临牌临时数据
    getTmpPlate() {
        if (!this.tmpPlate) {
            this.tmpPlate = {};
        }
        return this.tmpPlate;
    },
    // 设置临牌临时数据
    setTmpPlate(info) {
        const data = this.getTmpPlate();
        this.tmpPlate = Object.assign(data, info);
    }
})